package com.arcvideo.acsconnect.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arcvideo.acsconnect.utils.DownLoadUtil;
import com.arcvideo.tyingyitonginhouse.R;
import com.framework.core.utils.ToastUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/28
 * @description
 */

public class UpgradeDialog extends Dialog {

    @BindView(R.id.tv_des)
    TextView mTvDes;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    private Activity mActivity;
    //    private OnConfirmListener mOnConfirmListener;
    private boolean isShow = false;
    private String mDownloadUrl;

    public UpgradeDialog(@NonNull Activity activity, String downloadUrl) {
        super(activity, R.style.Dialog);
        this.mActivity = activity;
        this.mDownloadUrl = downloadUrl;
        init();
    }

//    public void setOnConfirmListener(OnConfirmListener listener) {
//        this.mOnConfirmListener = listener;
//    }

    private void init() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.layout_dialog_upgrade, null, false);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ButterKnife.bind(this, view);
        addContentView(view, layoutParams);

        Window window = this.getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = window.getAttributes();
        p.width = (int) (d.getWidth() * 0.35);
        p.height = (int) (d.getHeight() * 0.4);
        window.setAttributes(p);
        setCancelable(false);
    }

    @OnClick({R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                mTvConfirm.setVisibility(View.GONE);
                mTvDes.setText("下载中...");
//                if (null != mOnConfirmListener) {
//                    mOnConfirmListener.onConfirm();
//                }
                downloadApk(mDownloadUrl);
                break;
        }
    }

    private void downloadApk(String downloadUrl) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            ToastUtil.showToast("正在下载中");
            if (!isShow) {
                downloadAPK(downloadUrl);
            }
            isShow = true;
        } else {
            ToastUtil.showToast("sd卡不可用,下载失败");
        }
    }

    public void installApk(String apkFile) {
        File file = new File(apkFile);
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        mActivity.startActivity(intent);
    }


    public void downloadAPK(String downloadUrl) {
        DownLoadUtil.downloadAPK(downloadUrl, new DownLoadUtil.DownLoadListener() {

            @Override
            public void downloadFail() {
                isShow = false;
                ToastUtil.showToast("下载失败");
                //下载失败，去掉弹框，可以继续使用老版本
                dismiss();
            }

            @Override
            public void downloadComplete(long fileSize, long downLoadFileSize, String apkFile) {
//                manager.cancel(0);
                isShow = false;
//                changeConfirmDownloadBtnStatus(isShow);
                installApk(apkFile);
                ToastUtil.showToast("下载完成");
                //下载完成，去掉弹框
                dismiss();
            }

            @Override
            public void download(long fileSize, long downLoadFileSize) {
                mTvDes.setText("下载中..." + (downLoadFileSize * 100 / fileSize) + "%");
            }
        });
    }

//    public interface OnConfirmListener {
//        void onConfirm();
//    }

}
